package ics.zychen;

import ics.zychen.auth.SinaOAuthConstant;
import ics.zychen.sql.RenrenDataHelper;
import ics.zychen.sql.SinaDataHelper;
import ics.zychen.sql.UserInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.exception.RenrenException;
import com.renren.api.connect.android.status.StatusSetRequestParam;

import weibo4android.Weibo;
import weibo4android.WeiboException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Test3Activity extends Activity{
	private static final int INIT_ACC_END = 0;
	ProgressDialog pd;
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case INIT_ACC_END:
				ListView list = (ListView) findViewById(R.id.account_list);
				
				list.setAdapter((AccountListAdapter) (msg.obj));
				pd.dismiss();
				break;
			}
		}
	};
	
	private List<UserInfo> allUsers = new ArrayList<UserInfo>();
	private Vector<Integer> selectedUsers = new Vector<Integer>();
	private void init_renren_account(ArrayList<HashMap<String, Object>> listItem){
		RenrenDataHelper helper = new RenrenDataHelper(Test3Activity.this);
		List<UserInfo> users = helper.GetUserList(false);
		for(UserInfo user : users){
			HashMap<String, Object> rr = new HashMap<String, Object>();
			rr.put("ItemText", user.getUserName());
			rr.put("ItemImage", user.getUserIcon());
			rr.put("ItemText2", this.getString(R.string.sns_renren));
			listItem.add(rr);
			user.setUserType("renren");
			allUsers.add(user);
		}
		helper.Close();
	}
	private void init_sina_account(ArrayList<HashMap<String, Object>> listItem){
    	
        SinaDataHelper helper = new SinaDataHelper(Test3Activity.this);
		List<UserInfo> users = helper.GetUserList(false);
		for(UserInfo user : users){
			HashMap<String, Object> sina = new HashMap<String, Object>();
			sina.put("ItemText", user.getUserName());
			sina.put("ItemImage", user.getUserIcon());
			sina.put("ItemText2", this.getString(R.string.sns_sina));
			listItem.add(sina);
			user.setUserType("sinaweibo");
			allUsers.add(user);
		}
		helper.Close();
	}
	
	
	private void initAccount(){
		new Thread(new Runnable() {
			public void run() {
				ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();
				
				init_sina_account(listItem);
				init_renren_account(listItem);
				
				AccountListAdapter listItemAdapter = new AccountListAdapter(Test3Activity.this, listItem, selectedUsers);
				
				Message message = new Message();
				message.what = INIT_ACC_END;
				message.obj = listItemAdapter;
				mHandler.sendMessage(message);
		}}).start();
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        pd = new ProgressDialog(this);
        pd.setCancelable(true);
        pd.setMessage("loading sns account...");
        pd.show();
        initAccount();
        
        Button accountAddButton = (Button) findViewById(R.id.btn_addaccount);
        accountAddButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Test3Activity.this, SnsSelectActivity.class);
				startActivity(intent);
			}
		});
        
        Button btnStatusPublish = (Button) findViewById(R.id.status_publish);
        
        
        btnStatusPublish.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText txtStatus = (EditText) findViewById(R.id.status_text);
				String status_text = txtStatus.getText().toString();
				Log.e("StatusPublish", status_text);
				Log.e("SelectedUser", selectedUsers.toString());
				
				for(int index : selectedUsers){
					UserInfo user = allUsers.get(index);
					if(user.getUserType().equals("sinaweibo")){
						System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
				    	System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
						SinaOAuthConstant.getInstance().setToken(user.getToken());
						SinaOAuthConstant.getInstance().setTokenSecret(user.getTokenSecret());
						Weibo weibo = SinaOAuthConstant.getInstance().getWeibo();
						weibo.setToken(user.getToken(), user.getTokenSecret());
						try {
							weibo.updateStatus(status_text);
						} catch (WeiboException e) {
							e.printStackTrace();
						}
					} else if(user.getUserType().equals("renren")){
						final String API_KEY = "f79cfacaa4e943349fd23458b2fe3d5d";
						final String SECRET_KEY = "3a640ad285cc4f4fbd5ddc06640537be";
						final String APP_ID = "197463";
						Renren renren = new Renren(API_KEY, SECRET_KEY, APP_ID, Test3Activity.this);
						
						try {
							renren.publishStatus(new StatusSetRequestParam(status_text));
						} catch (RenrenException e) {
							e.printStackTrace();
						} catch (Throwable e) {
							e.printStackTrace();
						}
					}
				}
			}
		});
    }
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Log.e("Test3Activity", "onNewIntent");
        pd.show();
		initAccount();
	}
    
}